<template>
  <div class="wrapper">
    <img
      src="http://www.dell-lee.com/imgs/vue3/user.png"
      alt=""
      class="wrapper__userImg"
    />
    <div class="wrapper__inputBox">
      <input
        type="text"
        class="wrapper__inputBox__input"
        placeholder="请输入用户名"
        v-model="username"
      />
      <div class="wrapper__inputBox__gap"></div>
      <input
        type="password"
        class="wrapper__inputBox__input"
        placeholder="请输入密码"
        v-model="password"
        autocomplete="new-password"
      />
    </div>
    <button class="wrapper__btn" @click="handleLogin">登录</button>
    <div class="wrapper__newUser">
      <a
        href="javascript:;"
        class="wrapper__newUser__link"
        @click="handleRegisterClick"
        >立即注册
      </a>
      <a href="javascript:;" class="wrapper__newUser__gap"></a>
      <a href="javascript:;" class="wrapper__newUser__link">忘记密码</a>
    </div>
    <Toast v-if="show" :message="toastMessage" />
  </div>
</template>

<script>
import { reactive, toRefs } from "vue";
import { useRouter } from "vue-router";
import { post } from "../../utils/request";
import Toast, { useToastEffect } from "../../components/Toast";

//处理登录相关逻辑
const useLoginEffect = (showToast) => {
  const router = useRouter();
  const data = reactive({ username: "", password: "" });
  const handleLogin = async () => {
    try {
      const { username, password } = data;
      if(!username || !password) {
        showToast("用户名或密码不正确");
        return
      }
      const result = await post("/api/user/login/", {
        username: data.username,
        password: data.password,
      });
      if (result?.errno === 0) {
        localStorage.isLogin = true;
        //利用路由的push方法跳转到指定的页面
        router.push({ name: "Home" });
      } else {
        //调用已经封装好的弹窗函数
        showToast("登陆失败");
      }
    } catch (e) {
      //调用已经封装好的弹窗函数
      showToast("请求失败");
    }
  };
  const { username, password } = toRefs(data);
  return { username, password, handleLogin };
};

const useRegisterEffect = () => {
  const router = useRouter();
  const handleRegisterClick = () => {
      router.push({ name: "Register" });
    };
    return { handleRegisterClick }
}

export default {
  name: "Login",
  components: { Toast },
  //setup的职责就是告诉自己代码执行的一个流程
  setup() {
    //这是公共部分的弹窗功能，只要提示登录失败或请求失败的逻辑
    const { show, toastMessage, showToast } = useToastEffect();
    //用户和密码上的处理数据逻辑以及登录过程的一个函数封装
    const { username, password, handleLogin } = useLoginEffect(showToast);
    //登录界面上的立即注册跳转逻辑
    const { handleRegisterClick } = useRegisterEffect()
    return {
      username,
      password,
      show,
      toastMessage,
      handleLogin,
      handleRegisterClick,
    };
  },
};
</script>

<style lang="scss" scoped>
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  padding: 0 0.4rem;
  &__userImg {
    width: 0.66rem;
    height: 0.66rem;
    display: block;
    margin: 0 auto 0.4rem;
  }
  &__inputBox {
    display: flex;
    flex-wrap: wrap;
    &__input {
      width: 100%;
      padding: 0.12rem 0 0.12rem 0.16rem;
      border: 0.01rem solid rgba(0, 0, 0, 0.1);
      border-radius: 0.06rem;
      background-color: #f9f9f9;
      font-size: 0.16rem;
      line-height: 0.24rem;
      color: rgba(0, 0, 0, 0.5);
      &::placeholder {
        color: rgba(0, 0, 0, 0.5);
      }
    }
    &__gap {
      width: 100%;
      height: 0.16rem;
    }
  }
  &__btn {
    width: 100%;
    margin: 0.16rem auto;
    padding: 0.12rem 0;
    font-size: 0.16rem;
    color: #ffffff;
    line-height: 0.24rem;
    border-radius: 0.04rem;
    background-color: #0091ff;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
  }
  &__newUser {
    display: flex;
    justify-content: center;
    align-items: center;
    &__link {
      font-size: 0.14rem;
      color: rgba(0, 0, 0, 0.5);
      line-height: 0.2rem;
    }
    &__gap {
      height: 0.13rem;
      margin: 0 0.12rem;
      border-left: 0.01rem solid rgba(0, 0, 0, 0.5);
      border-right: 0.01rem solid rgba(0, 0, 0, 0.5);
    }
  }
}
</style>